package com.kyzh.core.pager.gamelist

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gushenge.core.beans.Sort
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.adapters.v3.GameRankBottomAdapter
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.uis.TabItems
import com.gushenge.core.toast
import kotlinx.android.synthetic.main.fragment_gamerank.*
import org.jetbrains.anko.textColor


class GameRankFragment : BaseFragment() {
    private var position: Int = 0
    private var list = arrayListOf<Sort>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            position = it.getInt("position")
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_gamerank, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ivTop.setImageResource(if (position==0)R.drawable.ic_reyou else R.drawable.ic_xinyou)
        initData()
    }

    private fun initData() {
        GameImpl.sort(object : ResultListener {
            override fun success(lists: Any) {

                lists as ArrayList<Sort>
                list.clear()
                list.addAll(lists)
                val adapter = GameRankBottomAdapter(R.layout.recyclerview, lists, position)
                viewpager.adapter = adapter
                viewpager.currentItem = 0
                viewpager.offscreenPageLimit = 1
                val  mediator = TabLayoutMediator(tabLayout,viewpager){ tab,position->
                    tab.text = lists[position].name
                    if (position==0){
                        tab.customView = TabItems.getGameRankTabView(requireContext(),position,list)
                        val textView = tab.customView?.findViewById<TextView>(R.id.text)
                        textView?.setBackgroundResource(R.drawable.bg_border_radius_default)
                        textView?.textColor = ContextCompat.getColor(requireContext(),R.color.colorPrimary)
                    }else{
                        tab.customView = TabItems.getGameRankTabView(requireContext(),position,list)
                        val textView = tab.customView?.findViewById<TextView>(R.id.text)
                        textView?.setBackgroundResource(R.drawable.bg_border_radius_cccccc)
                        textView?.textColor = Color.parseColor("#888888")
                    }
                }
                recoverItem()
                mediator.attach()

                tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                    override fun onTabSelected(tab: TabLayout.Tab?) {
                        recoverItem()
                        chooseTab(tab)
                    }
                    override fun onTabUnselected(tab: TabLayout.Tab?) {}
                    override fun onTabReselected(tab: TabLayout.Tab?) {}
                })
                viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        if (position !=0 ){
                            adapter.notifyItemChanged(position)
                        }
                    }
                })
            }
            override fun error(error: String) {
                error.toast()
            }
        })
    }

    companion object {
        @JvmStatic
        fun newInstance(position: Int) =
                GameRankFragment().apply {
                    arguments = Bundle().apply {
                        putInt("position", position)
                    }
                }
    }
    private fun recoverItem() {
        for (i in 0 until list.size){
            val textView = tabLayout?.getTabAt(i)?.view?.findViewById<TextView>(R.id.text)
            textView?.setBackgroundResource(R.drawable.bg_border_radius_cccccc)
            textView?.textColor = Color.parseColor("#888888")
        }
    }

    private fun chooseTab(tab: TabLayout.Tab?) {
        val textView = tab?.view?.findViewById<TextView>(R.id.text)
        textView?.setBackgroundResource(R.drawable.bg_border_radius_default)
        textView?.textColor = ContextCompat.getColor(requireContext(),R.color.colorPrimary)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }
}